﻿using System;
using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using UnityEngine.UI;

/**
 * 强化界面
 */
public class StrongWnd : WindowRoot{

    #region UIDefine

    public Image imgCurPos;
    public Text txtStarLv;
    public Transform startTranGrp;
    public Text propHp1;
    public Text propHurt1;
    public Text propDef1;
    public Text propHp2;
    public Text propHurt2;
    public Text propDef2;
    public Image propArror1;
    public Image propArror2;
    public Image propArror3;

    public Text txtNeedLv;
    public Text txtCostCoin;
    public Text txtCostCrystal;
    public Text txtCoin;
    public Transform costTransRoot;
    public Text txtStrong;
    
    #endregion

    public Transform posBtnTrans;

    private Image[] images = new Image[6];

    private int currentIndex = 0;

    private PlayerData pd;

    private StrongCfg nextSc;
    
    protected override void InitWnd(){
        base.InitWnd();
        pd = GameRoot.Instance.PlayerData;
        PECommon.Log("Init StrongWnd done");
        RegClickEvents();
        RefreshItem();
    }

    private void RegClickEvents(){
        for (int i = 0; i < posBtnTrans.childCount; i++){
            Image img = posBtnTrans.GetChild(i).GetComponent<Image>();
            OnClick(img.gameObject, (object obj) => {
                ClickPosItem((int)obj);
                audioSvc.PlayUIMusic(Constants.UIClickBtn);
            }, i);
            images[i] = img;
        }
    }

    private void ClickPosItem(int index){
        PECommon.Log("Click item" + index);
        currentIndex = index;
        for (int i = 0; i < images.Length; i++){
            Transform trans = images[i].transform;
            if (i == currentIndex){
                //箭头显示
                SetSprite(images[i], PathDefine.ItemArrorBG);
                trans.localPosition = new Vector3(-373, trans.localPosition.y);
                trans.GetComponent<RectTransform>().sizeDelta = new Vector2(250, 100);
            }
            else{
                SetSprite(images[i], PathDefine.ItemPlatBG);
                trans.localPosition = new Vector3(-384, trans.localPosition.y);
                trans.GetComponent<RectTransform>().sizeDelta = new Vector2(220, 80);
            }
        }

        RefreshItem();
    }

    private void RefreshItem(){
        //金币
        SetText(txtCoin, pd.coin);
        //装备精灵
        switch (currentIndex){
            case 0:
                SetSprite(imgCurPos, PathDefine.ItemToukui);       
                break;
            case 1:
                SetSprite(imgCurPos, PathDefine.ItemBody);       
                break;
            case 2:
                SetSprite(imgCurPos, PathDefine.ItemWaist);       
                break;
            case 3:
                SetSprite(imgCurPos, PathDefine.ItemHand);       
                break;
            case 4:
                SetSprite(imgCurPos, PathDefine.ItemLeg);       
                break;
            case 5:
                SetSprite(imgCurPos, PathDefine.ItemFoot);       
                break;
        }
        //星级
        SetText(txtStarLv, pd.strongArr[currentIndex] + "星级");
        //星级图片
        int curStarLv = pd.strongArr[currentIndex];
        for (int i = 0; i < startTranGrp.childCount; i++){
            Image img = startTranGrp.GetChild(i).GetComponent<Image>();
            if (i < curStarLv){
                SetSprite(img, PathDefine.SpStar2);
            }
            else{
                SetSprite(img, PathDefine.SpStar1);
            }
        }
        
        //强化数值
        int nextStarLv = curStarLv + 1;
        int sumAddHp = resSvc.GetPropAddValPreLv(currentIndex, nextStarLv, 1);
        int sumAddHurt = resSvc.GetPropAddValPreLv(currentIndex, nextStarLv, 2);
        int sumAddDef = resSvc.GetPropAddValPreLv(currentIndex, nextStarLv, 3);
        SetText(propHp1, sumAddHp);
        SetText(propHurt1, sumAddHurt);
        SetText(propDef1, sumAddDef);

        nextSc = resSvc.GetStrongData(currentIndex, nextStarLv);
        if (nextSc != null){
            SetActive(propHp2);
            SetActive(propHurt2);
            SetActive(propDef2);
            SetActive(costTransRoot);
            SetActive(propArror1);
            SetActive(propArror2);
            SetActive(propArror3);
            
            SetText(propHp2, "+" + nextSc.addhp);
            SetText(propHurt2, "+" + nextSc.addhurt);
            SetText(propDef2, "+" + nextSc.adddef);
            SetActive(txtStrong);
            
            SetText(txtNeedLv, nextSc.minlv);
            SetText(txtCostCoin, nextSc.coin);
            SetText(txtCostCrystal, nextSc.crystal + "/" + pd.crystal);
        }
        else{
            SetActive(propHp2, false);
            SetActive(propHurt2, false);
            SetActive(propDef2, false);
            SetActive(costTransRoot, false);
            SetActive(propArror1, false);
            SetActive(propArror2, false);
            SetActive(propArror3, false);
            SetActive(txtStrong, false);
        }
    }

    public void UpdateUI(){
        audioSvc.PlayUIMusic(Constants.FBIItemEnter);
        ClickPosItem(currentIndex);
    }
    
    public void ClickCloseBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        SetWndState(false);
    }

    public void ClickStrongBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);

        if (pd.strongArr[currentIndex] < 10){
            if (pd.level < nextSc.minlv){
                GameRoot.AddTips("角色等级不够");
                return;
            }
            if (pd.coin < nextSc.coin){
                GameRoot.AddTips("角色金币不够");
                return;
            }
            if (pd.crystal < nextSc.crystal){
                GameRoot.AddTips("角色水晶不够");
                return;
            }
            
            netSvc.SendMsg(new GameMsg{
                cmd = (int)CMD.ReqStrong,
                reqStrong = new ReqStrong{
                    pos = currentIndex
                }
            });
        }
        else{
            GameRoot.AddTips("星级已经升满");
        }
    }
}
